package com.panfeng.xcloud.common.core.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
public class TimeUtil {
    /**
     * 现在至明天0点的时间戳
     * @return
     */
    public static long nowToNext(){

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        long nowToNext = calendar.getTime().getTime()-System.currentTimeMillis();
        return nowToNext;
    }

    /**
     * 获取几天后的时间
     */
    public static Date getDateAfter(Date now,int day){
        Calendar calendar =Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.DATE,day);
        return calendar.getTime();
    }

    public static String getDateAfterStr(Date now,int day){
        Calendar calendar =Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.DATE,day);
        Date date = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    public static String formatDateToStr(Date date){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    /**
     * 获取几分钟后的时间
     */
    public static Date getMinuteAfter(Date now,int minute){
        Calendar calendar =Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.MINUTE,minute);
        return calendar.getTime();
    }

    /**
     * 获取几秒后的时间
     */
    public static Date getSecondAfter(int second){
        Calendar calendar =Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.SECOND,second);
        return calendar.getTime();
    }

    /**
     * 英文简写（默认）如：2010-12-01
     */
    public static String FORMAT_SHORT = "yyyy-MM-dd";
    /**
     * 英文全称  如：2010-12-01 23:15:06
     */
    public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
    /**
     * 精确到毫秒的完整时间    如：yyyy-MM-dd HH:mm:ss.S
     */
    public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
    /**
     * 中文简写  如：2010年12月01日
     */
    public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
    /**
     * 中文全称  如：2010年12月01日  23时15分06秒
     */
    public static String FORMAT_LONG_CN = "yyyy年MM月dd日  HH时mm分ss秒";
    /**
     * 精确到毫秒的完整中文时间
     */
    public static String FORMAT_FULL_CN = "yyyy年MM月dd日  HH时mm分ss秒SSS毫秒";


    public static void main(String[] args) {
        System.out.println(getTimeString());
        System.out.println("返回日期年份:"+getYear(new Date()));
        System.out.println("返回月份："+getMonth(new Date()));
        System.out.println("返回当天日份"+getDay(new Date()));
        System.out.println("返回当天小时"+getHour(new Date()));
        System.out.println("返回当天分"+getMinute(new Date()));
        System.out.println("返回当天秒"+getSecond(new Date()));
        System.out.println("返回当天毫秒"+getMillis(new Date()));
    }



    /**
     * 获取当前时间
     */
    public static String getTimeString() {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
        Calendar calendar = Calendar.getInstance();
        return df.format(calendar.getTime());
    }

    /**
     * 获取日期年份
     * @param date 日期
     * @return
     */
    public static String getYear(Date date) {
        return format(date).substring(0, 4);
    }

    private static String format(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
        return df.format(date);
    }

    /**
     * 功能描述：返回月
     *
     * @param date
     *            Date 日期
     * @return 返回月份
     */
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 功能描述：返回日期
     *
     * @param date
     *            Date 日期
     * @return 返回日份
     */
    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 功能描述：返回小时
     *
     * @param date
     *            日期
     * @return 返回小时
     */
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 功能描述：返回分
     *
     * @param date
     *            日期
     * @return 返回分钟
     */
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 返回秒钟
     *
     * @param date
     *            Date 日期
     * @return 返回秒钟
     */
    public static int getSecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }

    /**
     * 功能描述：返回毫
     *
     * @param date
     *            日期
     * @return 返回毫
     */
    public static long getMillis(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }

    static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 获取 yyyy-MM--dd类型的日期
     * @return
     */
    public static String getDate(){
        Date date = new Date();
        String dateString = simpleDateFormat.format(date);
        return dateString;
    }

    /**
     * 获取时间差，格式：xx天前，xx天xx小时前，xx小时前，xx小时xx分钟前，xx分钟前，刚刚
     * @param beginTime
     * @param endTime
     * @return
     */
    public static String timeToTime(Date beginTime,Date endTime){
        long time = endTime.getTime() - beginTime.getTime();
        log.info(">>>现在的时间:{}，数据库的发布时间:{}，时间差(毫秒):{}<<<",endTime,beginTime,time);

        long dayToMilliseconds = TimeUnit.DAYS.toMillis(1);
        long hoursToMilliseconds = TimeUnit.HOURS.toMillis(1);
        long minutesToMilliseconds = TimeUnit.MINUTES.toMillis(1);
        long day;
        long hours;
        long minutes;
        String publishTimeStr = null;
        if(time>=dayToMilliseconds){
            day = time/dayToMilliseconds;
            hours = TimeUnit.HOURS.convert(time-day*dayToMilliseconds,TimeUnit.MILLISECONDS);
            if(hours==0){
                publishTimeStr = day+"天前";
            }else{
                publishTimeStr = day+"天"+hours+"小时前";
            }
        }else if(time>=hoursToMilliseconds){
            hours = time/hoursToMilliseconds;
            minutes = TimeUnit.MINUTES.convert(time-hours*hoursToMilliseconds,TimeUnit.MILLISECONDS);
            if(minutes==0){
                publishTimeStr = hours+"小时前";
            }else{
                publishTimeStr = hours+"小时"+minutes+"分钟前";
            }
        }else if(time>=minutesToMilliseconds){
            minutes = time/minutesToMilliseconds;
            publishTimeStr = minutes+"分钟前";
        }else{
            publishTimeStr = "刚刚";
        }
        return publishTimeStr;
    }


    public static Long transforDateToLong(Date date) {
        long longTime = getMillis(date);
        return longTime;
    }
}
